package com.heatup.apt.management.service.order.impl;

import com.heatup.apt.common.dao.CourseSignMapper;
import com.heatup.apt.common.dao.OrderDetailMapper;
import com.heatup.apt.common.dao.OrderInfoMapper;
import com.heatup.apt.common.dao.coach.CoachInfoMapper;
import com.heatup.apt.common.dao.user.UserInfoMapper;
import com.heatup.apt.management.service.order.*;
import com.heatup.apt.common.util.DateUtils;
import com.heatup.apt.common.util.SmsUtil;
import com.heatup.apt.model.coach.CoachInfo;
import com.heatup.apt.model.course.CourseSign;
import com.heatup.apt.model.order.OrderDetail;
import com.heatup.apt.model.order.OrderInFoToTK;
import com.heatup.apt.model.order.OrderInfo;
import com.heatup.apt.model.order.YearCardOrderDetailManagementModel;
import com.heatup.apt.model.user.UserInfo;

import org.springframework.beans.factory.annotation.*;
import org.springframework.stereotype.*;

import java.text.*;
import java.util.*;

/**
 * Created by earvin on 2017/8/4.
 */
@Service
public class OrderManageServiceImpl implements OrderManageService {

    @Autowired
    private OrderInfoMapper orderInfoMapper;
    
    @Autowired
    private OrderDetailMapper orderDetailMapper;
    
    @Autowired
    private CoachInfoMapper coachInfoMapper;
    
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
	private CourseSignMapper courseSignMapper;
    
    DecimalFormat format = new DecimalFormat("###,####0.00");
    @Override
    public Map<String, Object> getTeamOrderList(Map<String, Object> param) {
        Map<String,Object> respMap = new HashMap<String,Object>();
        Map<String,Object> paramMap = new HashMap<>();
        Date now = new Date();
        // 获取到所有的团课订单
        String type = (String)param.get("type");
        paramMap.put("no_refund","1");
        paramMap.put("course_stime",param.get("course_stime"));
        paramMap.put("course_etime",param.get("course_etime"));
        paramMap.put("course_id",param.get("course_id"));
        paramMap.put("mobile",param.get("mobile"));
        paramMap.put("pageNum",param.get("pageNum"));
        paramMap.put("pageCount",param.get("pageCount"));
        paramMap.put("tradeId",param.get("tradeId"));
        paramMap.put("coachId",Integer.valueOf((String) param.get("coachId")));
        paramMap.put("userId", param.get("userId"));
        paramMap.put("userName", param.get("userName"));
        String store_ids = (String) param.get("store_id");
        int store_id = Integer.valueOf(store_ids);
        if(store_id != 0){
            paramMap.put("store_id",store_id);
        }
        // 已付款待开始
        if("1".equals(type)){
            String start_time = "";
            Date newTime = new Date();
            start_time	= DateUtils.format(newTime,DateUtils.datePatternAllHH);
            paramMap.put("start_time",start_time);
            paramMap.put("pay_status",1);
            paramMap.put("order_status",1);
            paramMap.put("cancel",0);
        }
        // 已付款排队中
        else if("2".equals(type)){
            paramMap.put("order_status",3);
            paramMap.put("pay_status",1);
            paramMap.put("cancel",0);
        }
        // 已结束
        else if("3".equals(type)){
            String end_time = "";
            Date newTime = new Date();
            end_time	= DateUtils.format(newTime,DateUtils.datePatternAllHH);
            paramMap.put("end_time",end_time);
            paramMap.put("pay_status",1);
            paramMap.put("order_status",4);
            paramMap.put("cancel",0);
        }
        // 主动取消
        else if("4".equals(type)){
            paramMap.put("order_status",2);
            paramMap.put("pay_status",1);
            paramMap.put("cancel",1);
        }
        // 待支付
        else if("5".equals(type)){
            paramMap.put("pay_status",2);
        }
        // 已退款
        else if("6".equals(type)){
            paramMap.put("refund","1");
            paramMap.remove("no_refund");
            paramMap.remove("pay_status");
            paramMap.put("cancel_status", "4");
        }
        // 被动取消
        else if("7".equals(type)){
            paramMap.put("order_status",2);
            paramMap.put("pay_status",1);
            paramMap.put("cancel",2);
        }

        OrderInFoToTK orderInFoToTK = orderInfoMapper.getOrderListCountByTK(paramMap);
        Integer pageCount = Integer.valueOf(0);
        if(orderInFoToTK !=null){
            pageCount=orderInFoToTK.getTotalCount();
            respMap.put("pageCount",pageCount);
            respMap.put("totalPayment",format.format(orderInFoToTK.getTotalPayment()));
            respMap.put("totalFee",format.format(orderInFoToTK.getTotalFee()));
        }
        List<OrderInFoToTK> teamOrderList = orderInfoMapper.getOrderListByTK(paramMap);
        respMap.put("TkData",teamOrderList);
        
        return respMap;
    }
    
    /**
     * 获取私教列表服务
     */
	@Override
	public Map<String, Object> getPersonalOrderList(Map<String, Object> param) {
		List<OrderInFoToTK> listO = new ArrayList<OrderInFoToTK>();
		Map<String,Object> mapWhere = new HashMap<String,Object>();
		String type = (String)param.get("type");
		mapWhere.put("course_stime",param.get("course_stime"));
		mapWhere.put("course_etime",param.get("course_etime"));
		mapWhere.put("course_id",param.get("course_id"));
		mapWhere.put("mobile",param.get("mobile"));
		mapWhere.put("pageNum",param.get("pageNum"));
		mapWhere.put("pageCount",param.get("pageCount"));
		mapWhere.put("tradeId",param.get("tradeId"));
		mapWhere.put("coachId",Integer.valueOf((String) param.get("coachId")));
		mapWhere.put("userId", param.get("userId"));
		mapWhere.put("userName", param.get("userName"));
		String store_ids = (String) param.get("store_id");
		int store_id = Integer.valueOf(store_ids);
		if(store_id != 0){
			mapWhere.put("store_id",store_id);
		}
		// 已支付
		if("1".equals(param.get("type"))){
			mapWhere.put("pay_status",1);
			mapWhere.put("order_status",1);
		}
		// 已结束
		else if("2".equals(param.get("type"))){
			mapWhere.put("pay_status",1);
			mapWhere.put("order_status",4);
		}
		// 待支付
		else if("3".equals(param.get("type"))){
			mapWhere.put("pay_status",2);
		}
		// 已退款 
		else if("4".equals(param.get("type"))){
			mapWhere.put("refund","success");
			mapWhere.put("pay_status",1);
			mapWhere.remove("pay_status");
			mapWhere.put("cancel_status", "4");
		}
		
		listO = orderInfoMapper.getOrderListBySJ(mapWhere);
		param.put("SjData", listO);
		OrderInFoToTK orderInFoToTK = orderInfoMapper.queryPersonaOrderCount(mapWhere);
        Integer pageCount = Integer.valueOf(0);
        if(orderInFoToTK !=null){
            pageCount=orderInFoToTK.getTotalCount();
            param.put("pageCount",pageCount);
            param.put("totalPayment",format.format(orderInFoToTK.getTotalPayment()));
            param.put("totalFee",format.format(orderInFoToTK.getTotalFee()));
        }
		return param;
	}
	
	/**
	 * 获取檬营列表
	 */
	@Override
	public Map<String, Object> getFunCampOrderList(Map<String, Object> param) {
		Map<String,Object> mapWhere = new HashMap<String,Object>();
		List<OrderInFoToTK> listDate = new ArrayList<OrderInFoToTK>();
		Integer pageNum = (Integer) param.get("pageNum"); 
		String course_stime =(String) param.get("course_stime");
		String course_etime =(String) param.get("course_etime");
		String course_id = (String)param.get("course_id");
		String mobile = (String)param.get("mobile");
		String store_ids = (String)param.get("store_id");
		System.out.println(store_ids);
		int store_id = Integer.valueOf(store_ids);
		mapWhere.put("pay_status","1");
		mapWhere.put("course_etime",course_etime);
		mapWhere.put("course_stime",course_stime);
		mapWhere.put("course_id",course_id);
		mapWhere.put("mobile",mobile);
		mapWhere.put("order_status",'1');
		mapWhere.put("pageNum",pageNum);
		mapWhere.put("pageCount",param.get("pageCount"));
		mapWhere.put("tradeId",param.get("tradeId"));
		mapWhere.put("userId", param.get("userId"));
		mapWhere.put("userName", param.get("userName"));
		if(store_id != 0){
			mapWhere.put("store_id",store_id);
		}
		// 已支付 成营
		if("1".equals(param.get("type"))){
			mapWhere.put("chen","2");
			mapWhere.put("order_status",1);
		}
		// 已支付 未成营
		else if("2".equals(param.get("type"))){
			mapWhere.put("weichen","2");
			mapWhere.put("order_status",'1');
		}
		// 已结束
		else if("3".equals(param.get("type"))){
			String end_time = "";
			Date newTime = new Date();
			end_time	= DateUtils.format(newTime,DateUtils.datePatternAllHH);
			mapWhere.put("end_time",end_time);
			mapWhere.put("order_status",'4');
		}
		// 待支付
		else if("4".equals(param.get("type"))){
			mapWhere.put("pay_status",'2');
		}
		// 被动取消
		else if("5".equals(param.get("type"))){
			mapWhere.put("cancel","2");
			mapWhere.put("order_status",2);
			
		}
		// 已退款 
		else if("6".equals(param.get("type"))){
			mapWhere.put("tuikuan","1");
			mapWhere.put("order_status",'2');
			/*mapWhere.put("pay_status", "4");*/
			mapWhere.remove("pay_status");
			mapWhere.put("cancel_status", "4");
		}// 主动取消
		else if("7".equals(param.get("type"))){
			mapWhere.put("cancel","1");
			mapWhere.put("order_status",2);
		}// 
		else if("8".equals(param.get("type"))){
			
		}else if("9".equals(param.get("type"))){
			mapWhere.put("chen","2");
			mapWhere.put("order_status",3);
		}
		listDate = orderInfoMapper.getOrderListByMY(mapWhere);
		param.put("MyData", listDate);
		OrderInFoToTK orderInFoToTK = orderInfoMapper.queryFunCampOrderCount(mapWhere);
        Integer pageCount = Integer.valueOf(0);
        if(orderInFoToTK !=null){
            pageCount=orderInFoToTK.getTotalCount();
            param.put("pageCount",pageCount);
            param.put("totalPayment",format.format(orderInFoToTK.getTotalPayment()));
            param.put("totalFee",format.format(orderInFoToTK.getTotalFee()));
        }
		return param;
	}
	
	/**
	 * 获取自助列表
	 */
	@Override
	public Map<String, Object> getSelfOrderList(Map<String, Object> param) {
		Map<String,Object> mapWhere = new HashMap<String,Object>();
		List<OrderInFoToTK> listDate = new ArrayList<OrderInFoToTK>();
		
		Integer pageNum = (Integer) param.get("pageNum"); 
		String course_stime =(String) param.get("course_stime");
		String course_etime =(String) param.get("course_etime");
		String title = (String)param.get("title");
		String mobile = (String)param.get("mobile");
		String store_ids = (String)param.get("store_id");
		int store_id = Integer.valueOf(store_ids);
		mapWhere.put("pageNum",pageNum);
		mapWhere.put("pageCount",param.get("pageCount"));
		mapWhere.put("tradeId",param.get("tradeId"));
		mapWhere.put("userId", param.get("userId"));
		mapWhere.put("userName", param.get("userName"));
		
		mapWhere.put("course_etime",course_etime);
		mapWhere.put("course_stime",course_stime);
		mapWhere.put("title",title);
		mapWhere.put("mobile",mobile);
		if(store_id != 0){
			mapWhere.put("store_id",store_id);
		}
		
		mapWhere.put("pay_status",'1');
		// 已支付 
		if("1".equals(param.get("type"))){
			mapWhere.put("order_status","1");
		}
		// 已结束
		else if("2".equals(param.get("type"))){
			String end_time = "";
			Date newTime = new Date();
			end_time	= DateUtils.format(newTime,DateUtils.datePatternAllHH);
			mapWhere.put("end_time",end_time);
			mapWhere.put("pay_status",'1');
			mapWhere.put("order_status","4");
		}
		// 待支付
		else if("3".equals(param.get("type"))){
			mapWhere.put("pay_status",'2');
		}
		// 已取消
		else if("4".equals(param.get("type"))){
			mapWhere.put("quxiao",2);
			mapWhere.put("order_status","2");
		}
		// 已退款 
		else if("5".equals(param.get("type"))){
			mapWhere.put("tuikuan",2);
			mapWhere.put("order_status","2");
			mapWhere.remove("pay_status");
			mapWhere.put("cancel_status", "4");
		}
		listDate = orderInfoMapper.getOrderListByZZ(mapWhere); 
		param.put("ZzData", listDate);
		OrderInFoToTK orderInFoToTK = orderInfoMapper.querySelfOrderCount(mapWhere);
        if(orderInFoToTK !=null){
            param.put("pageCount",orderInFoToTK.getTotalCount());
            param.put("totalPayment",format.format(orderInFoToTK.getTotalPayment()));
            param.put("totalFee",format.format(orderInFoToTK.getTotalFee()));
        }
		return param;
	}
	
	

    /**
     * 私教体验课列表查询
     * @param param
     * @return
     */
    @Override
    public Map<String, Object> getSeOrderList(Map<String, Object> param) {
		Map<String, Object> respMap = new HashMap();
		String type = (String) param.get("type");
		// 已结束
		if ("2".equals(type)) {
			param.put("orderStatus", "4");
			param.put("payStatus", 1);
		} else if ("3".equals(type)) {
			// 已取消
			param.put("orderStatus", 2);
			param.put("payStatus", 1);
		} else if ("4".equals(type)) {
			// 已退款
			param.put("payStatus", 4);
		} else if ("5".equals(type)) {
			// 未支付
			param.put("payStatus", 2);
		} else {
			param.put("orderStatus", "1");
			param.put("payStatus", 1);
		}

		List<OrderInFoToTK> list = orderInfoMapper.querySeAndPeOrderList(param);
		respMap.put("SeOrderList", list);
		OrderInFoToTK countOrder = orderInfoMapper.querySeAndPeOrderCount(param);
		respMap.put("totalCount", countOrder.getTotalCount());
		respMap.put("totalPayment", countOrder.getTotalPayment());
		respMap.put("totalFee", countOrder.getTotalFee());
		return respMap;
    }
    
    /**
     * 获取私教预约订单列表
     * 私教咨询
     */
	@Override
	public Map<String, Object> getPrivateBespeakOrderList(Map<String, Object> param) {
		Map<String, Object> respMap = new HashMap();
		List<OrderInFoToTK> list = orderInfoMapper.queryPrivateBespeakOrderList(param);
		respMap.put("SeOrderList", list);
		Integer orderCount = orderInfoMapper.queryPrivateBespeakOrderCount(param);
		respMap.put("totalCount", orderCount);
		
		return respMap;
	}
	
	/**
	 * 转单  1:体测 2:体验
	 */
	@Override
	public Integer transferenceOrder(Map<String, Object> param) {

		Integer orderId 	= (Integer) param.get("orderId");
		Integer coachId 	= (Integer) param.get("coachId");
		String 	student  	= (String) param.get("student");
		Integer type  		= (Integer) param.get("type");
		OrderDetail orderDetail = orderDetailMapper.queryOrderDetailByOrderId(orderId);
		orderDetail.setCoachId(coachId);
		Integer updateCount = orderDetailMapper.updateSelectiveById(orderDetail);
		OrderInfo orderInfo = orderInfoMapper.selectById(orderId);
		UserInfo userInfo 	= userInfoMapper.selectById(orderInfo.getUserId());
		CoachInfo coachInfo = coachInfoMapper.selectById(coachId);

		// 还要将日程的教练修改掉
		int delCourseSignCount = courseSignMapper.delCourseSignByOrderId(orderId);

		if(updateCount > 0){
			SmsUtil.sendTemplateCoachPrivateOrderTransferNotificationSms(coachInfo.getNickName(), userInfo.getNickName(), type, coachInfo.getMobile());
		}
		return updateCount;
	}

	/**
	 * 根据订单
	 */
	@Override
	public Map<String, Object> getOrderListByOrderNumber(Map<String, Object> parma) {
		String orderNumber  = (String) parma.get("orderNumber");
		OrderInfo orderInfo = orderInfoMapper.queryOrderInfoByNumber(orderNumber);
		parma.put("orderInfo", orderInfo);
		return parma;
	}

	@Override
	public Map<String, Object> getOpenYearCardOrderDetail(Integer orderId) {
		YearCardOrderDetailManagementModel yearCardOrder = orderInfoMapper.getOpenYearCardOrderDetailByOrderId(orderId);
		Map<String,Object> resultMap = new HashMap<>();
		resultMap.put("orderDetail", yearCardOrder);
		return resultMap;
	}
}
